package com.ice.app.system.role.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iceframework.core.entity.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.system.entity.Role;
import com.ice.app.system.menu.service.MenuService;
import com.ice.app.system.menu.vo.MenuTreeVo;
import com.ice.app.system.role.service.RoleService;
import com.ice.app.system.role.vo.RoleTreeVo;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Result;

/**
 * 
 * 控制层： 角色
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private MenuService menuService;

	/**
	 * 获取数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getRole(@RequestParam(value = "key") String key, HttpServletRequest request) {
		Role role = roleService.getRole(key);
		return Result.success().add("role", role);
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param role
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	public Result roleIndexData(Role role, Page<Role> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<Role> roles = roleService.getRoles(role);
		PageInfo<Role> data = new PageInfo<Role>(roles, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param role
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result saveRole(Role role) throws Exception {
		if (roleService.saveRole(role)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Result delRole(String keys) {
		if (roleService.deleteRoles(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

	/**
	 * 获取角色树状列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/tree/list/data", method = RequestMethod.POST)
	public List<RoleTreeVo> getRoleTreeListData() {
		return roleService.getRoleTreeList();
	}

	/**
	 * 加载配置菜单树页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/resource/tree/data", method = RequestMethod.GET)
	public Result menuTreeIndexDataByRoleIdMenuType(@RequestParam(value = "key") String key, HttpServletRequest request) {
		List<MenuTreeVo> menuTree = menuService.getMenuTreeByRoleIdMenuType(key, "");
		return Result.success().add("data", menuTree);
	}

	/**
	 * 保存角色和菜单的联系
	 * 
	 * @param role
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/resource/tree/save", method = RequestMethod.POST)
	public Result saveRoleMenuRel(@RequestParam(value = "ids") String menuIds, @RequestParam(value = "key") String roleId, HttpServletRequest request) throws Exception {
		if (roleService.saveRoleMenuRel(roleId, menuIds)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

}